package com.xiyuan.util;

import java.text.DecimalFormat;

/**
 * 描述: 银行卡、手机、身份证加*的工具类，该类可被java和velocity同时调用
 * 
 * 提示: 银行卡，身份证，手机的加*操作可以放在这里。
 * 
 * 实例:6228121208006608481-->6228 **** **** **** 481
 * 
 * @author wjx
 */
public class ProcessUtil {

	/**
	 * 银行卡加* example 6228 **** **** **** 678
	 */
	public static String bankSign(String bankAccount) {

		if (bankAccount == null || bankAccount.length() <= 7)
			return bankAccount;
		int length = bankAccount.length();
		StringBuffer newAccount = new StringBuffer();
		newAccount.append(bankAccount.substring(0, 4));
		for (int i = 0; i < length - 7; i++) {
			newAccount.append("*");
		}
		newAccount.append(bankAccount.substring(length - 3));
		String result = bankSpace(newAccount.toString());
		return result;
	}

	/**
	 * 银行卡号加空格 example 622812345678678-->6228 1234 5678 678
	 */
	public static String bankSpace(String bankAccount) {
		if (bankAccount == null || bankAccount.length() == 0)
			return null;
		int length = bankAccount.length();
		if (length >= 13 && length < 17) {
			String newAccount = bankAccount.substring(0, 4) + " "
					+ bankAccount.substring(4, 8) + " "
					+ bankAccount.substring(8, 12) + " "
					+ bankAccount.substring(12);

			return newAccount;
		}
		if (length >= 17 && length <= 20) {
			String newAccount = bankAccount.substring(0, 4) + " "
					+ bankAccount.substring(4, 8) + " "
					+ bankAccount.substring(8, 12) + " "
					+ bankAccount.substring(12, 16) + " "
					+ bankAccount.substring(16);

			return newAccount;
		}
		return bankAccount;
	}

	/**
	 * 银行卡号去空格 example 6228 1234 5678 678-->622812345678678
	 */
	public static String bankNoSpace(String bank) {
		if (bank == null)
			return null;
		String newBank = "";
		String[] temp = bank.split(" ");
		for (int i = 0; i < temp.length; i++) {
			newBank = newBank + temp[i];
		}
		return newBank;
	}
	/**
	 * 邮箱@符号以前，只有第一个和最后一个字母显示，其他换为***
	 */
	public static String getEmailStr(String email) {
		if (email == null || email.length() <= 3)
			return email;
		int in = email.indexOf("@");
		if (in < 1)
			return email;
		String result = email.substring(0, 1) + "***" + email.substring(in - 1);
		return result;
	}
	
	/**
	 * 身份证，只显示最前面和最后面的数字
	 */
	public static String getCardIdStr(String cardId){
		if(cardId==null||cardId.length()<15)return cardId;
		int length = cardId.length();
		String result = cardId.substring(0,1)+"****************"+cardId.substring(length-1);
		return result;
	}
	
	public static String getNameStr(String name){
		if(name.length()<2)return name;
		String newName = "*"+name.substring(1);
		return newName;
	}
	/**
	 * 手机，显示前三位和后三位
	 */
	public static String getMobileStr(String mobile){
		if(mobile==null||mobile.length()<8)return mobile;
		String result= mobile.substring(0,3)+"*****"+mobile.substring(8);
		return result;
		//return mobile;
	}
	/**
	 *去空格
	 */
	public static String getStrNoSpace(String str){
		if(str==null)return str;
		String result= str.replace(" ", "");
		return result;
	}

	public static void main(String args[]) {
//		System.out.println(bankSign("1234567890123456789"));
//		System.out.println(getEmailStr("bphg@sina.com"));
//		System.out.println(getCardIdStr("330782198908160"));
		Double fee = 1.238;
		String a = new DecimalFormat("#.00").format(fee);
		System.out.println(a);
	}

}
